package com.showde.shoppingmall.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kim.androidbase.db.DBCommandInteface;
import com.kim.androidbase.db.DBSession;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;
import com.kim.androidbase.db.SQLiteCriteria;
import com.showde.shoppingmall.db.constant.ProductCategoryConstant;
import com.showde.shoppingmall.domain.ProductCategory;

public class ProductCategoryDBCommand implements
		DBCommandInteface<ProductCategory, String> {

	private static final String TAG = "ProductCategoryDBCommand";

	DBSession dbSession;

	public ProductCategoryDBCommand(Context context) {
		dbSession = new DBSession(context, new DBHelper(context));
	}

	@Override
	public void add(ProductCategory productCategory) {
		dbSession.insert(ProductCategoryConstant.TABLENAME,
				getContentValues(productCategory),
				SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void add(List<ProductCategory> ts) {
		List<ContentValues> contentValuesList = new ArrayList<ContentValues>();
		for (ProductCategory productCategory : ts) {
			contentValuesList.add(getContentValues(productCategory));
		}
		dbSession.insertBatch(ProductCategoryConstant.TABLENAME,
				contentValuesList, SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void delete(String pk) {
		dbSession.delete(ProductCategoryConstant.TABLENAME,
				ProductCategoryConstant.PRODUCTCATEGORYID + "=?",
				new String[] { pk });
	}

	@Override
	public void delete(SQLiteCriteria arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void modify(SQLiteCriteria criteria) {
		// TODO Auto-generated method stub

	}

	@Override
	public void modify(String arg0, SQLiteCriteria arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public ProductCategory query(String pk) {
		final String sql = "select * from " + ProductCategoryConstant.TABLENAME
				+ " where " + ProductCategoryConstant.COLUMN_PRODUCTCATEGORYID
				+ "=?";
		final ProductCategory productCategory = new ProductCategory();
		dbSession.query(sql, new String[] { pk }, new QueryCompleteCallback() {

			@Override
			public void onQueryComplete(Cursor cursor) {
				if (cursor != null && cursor.getCount() > 0
						&& cursor.moveToNext()) {
					productCategory.setProductCategoryId(dbSession.getString(
							cursor, ProductCategoryConstant.PRODUCTCATEGORYID));
					productCategory.setProductCategoryName(dbSession
							.getString(cursor,
									ProductCategoryConstant.PRODUCTCATEGORYNAME));
					productCategory.setProductCategoryIconUrl(dbSession
							.getString(
									cursor,
									ProductCategoryConstant.PRODUCTCATEGORYICONURL));
					productCategory.setProductCategoryDesc(dbSession
							.getString(cursor,
									ProductCategoryConstant.PRODUCTCATEGORYDESC));
					productCategory.setProductCategoryPid(dbSession.getString(
							cursor, ProductCategoryConstant.PRODUCTCATEGORYPID));
				}
			}

			@Override
			public void onQueryError(String errorMsg) {
				Log.d(TAG, "query:" + errorMsg);
			}
		});
		return productCategory.getProductCategoryId() == null ? null
				: productCategory;
	}

	@Override
	public List<ProductCategory> query(SQLiteCriteria arg0) {
		return null;
	}

	@Override
	public List<ProductCategory> queryAll() {
		final String sql = "select * from " + ProductCategoryConstant.TABLENAME;
		final List<ProductCategory> productCategories = new ArrayList<ProductCategory>();
		dbSession.query(sql, null, new QueryCompleteCallback() {

			@Override
			public void onQueryError(String errorMsg) {
				Log.d(TAG, "queryAll:" + errorMsg);
			}

			@Override
			public void onQueryComplete(Cursor cursor) {
				if (cursor != null && cursor.getCount() > 0) {
					ProductCategory productCategory = null;
					while (cursor.moveToNext()) {
						productCategory = new ProductCategory();
						productCategory.setProductCategoryId(dbSession
								.getString(
										cursor,
										ProductCategoryConstant.PRODUCTCATEGORYID));
						productCategory.setProductCategoryName(dbSession
								.getString(
										cursor,
										ProductCategoryConstant.PRODUCTCATEGORYNAME));
						productCategory.setProductCategoryIconUrl(dbSession
								.getString(
										cursor,
										ProductCategoryConstant.PRODUCTCATEGORYICONURL));
						productCategory.setProductCategoryDesc(dbSession
								.getString(
										cursor,
										ProductCategoryConstant.PRODUCTCATEGORYDESC));
						productCategory.setProductCategoryPid(dbSession
								.getString(
										cursor,
										ProductCategoryConstant.PRODUCTCATEGORYPID));
						productCategories.add(productCategory);
					}
				}
			}
		});
		return productCategories;
	}

	private ContentValues getContentValues(ProductCategory productCategory) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(ProductCategoryConstant.PRODUCTCATEGORYID,
				productCategory.getProductCategoryId());
		contentValues.put(ProductCategoryConstant.PRODUCTCATEGORYNAME,
				productCategory.getProductCategoryName());
		contentValues.put(ProductCategoryConstant.PRODUCTCATEGORYICONURL,
				productCategory.getProductCategoryIconUrl());
		contentValues.put(ProductCategoryConstant.PRODUCTCATEGORYDESC,
				productCategory.getProductCategoryDesc());
		contentValues.put(ProductCategoryConstant.PRODUCTCATEGORYPID,
				productCategory.getProductCategoryPid());
		return contentValues;
	}

}
